'use strict';
/*-------------------------------------------------------------
Author:SharpQian  
(C)shhoo.cn
******************* 执行器配置 ************************
接口：
    1、公共指令部分
    2、卷膜部分指令
    3、拉幕部分指令
    4、风机部分指令

-------------------------------------------------------------*/
import moment from 'moment';
moment.locale('zh-cn');
import { LoRaComCtrlClass } from "../device/loRaComCtrl"
const LoRaComCtrl = new LoRaComCtrlClass();
LoRaComCtrl.open();
import { TcpComCtrlClass } from "../device/tcpComCtrl"
const TcpComCtrl = new TcpComCtrlClass();
TcpComCtrl.open();
import { RedisGatewayClass } from "../lib/redis_gateway";
import ComCtrl from "../device/comCtrl";
import Roller from "../device/roller";
import Screen from "../device/screen";
import Fan from "../device/fan";
import S100 from "../device/S100";
import WS100 from "../device/WS100";
function Parser(RedisGateway: RedisGatewayClass, message: string) {
    try {
        let obj = JSON.parse(message);
        let confTyp = obj.confTyp;
        let method = obj.method;
        let qryId = obj.qryId == undefined ? "11111111111" : obj.qryId;
        let para = obj.para;
        // console.log(`${new Date().toLocaleString()}【actuatorCmd.ts/<Parser>】${new Date().toLocaleString()}`, obj)
        if (confTyp == "COM_CTRL") {//公共部分指令
            ComCtrl.Parser(RedisGateway, LoRaComCtrl, TcpComCtrl, confTyp, method, qryId, para);
        } else if (confTyp == "ROLLER" || (confTyp == "LORA_COMMON_CMD" && para.deviceType == "LoRa_Roller")) {//卷膜相关指令
            Roller.Parser(RedisGateway, LoRaComCtrl, TcpComCtrl, confTyp, method, qryId, para);
            // S100.Parser(RedisGateway, confTyp, method, qryId, para);
        } else if (confTyp == "SCREEN" || (confTyp == "LORA_COMMON_CMD" && para.deviceType == "LoRa_Screen")) {//拉幕相关指令
            Screen.Parser(RedisGateway, LoRaComCtrl, TcpComCtrl, confTyp, method, qryId, para);
        } else if (confTyp == "FAN" || (confTyp == "LORA_COMMON_CMD" && para.deviceType == "LoRa_Fan")) {//风机相关指令
            Fan.Parser(RedisGateway, LoRaComCtrl, TcpComCtrl, confTyp, method, qryId, para);
        }

    } catch (error) {
        console.log(`${new Date().toLocaleString()}【actuatorCmd.ts/<Parser>】${new Date().toLocaleString()}`, error.message)
    }
}

let RedisGateway = new RedisGatewayClass();
RedisGateway.on("message", _onReceive);

function _onReceive(channel: string, message: string) {
    // console.log(channel, message)
    if (channel == 'SERIAL_CMD_ACK') {
        // console.log(`${new Date().toLocaleString()}`, channel, serialAckParams.msg, serialAckParams.recvData)
        try {
            let serialAckParams = JSON.parse(message);
            let addr = serialAckParams.addr;
            let glsnr = serialAckParams.lsnr;
            let grssi = serialAckParams.rssi;
            let recvData = serialAckParams.recvData;
            let freameData = Buffer.from(recvData, 'hex');//帧数据
            let frameId = freameData.slice(1, 3).toString('hex').toUpperCase();//帧Id
            let deviceTypeId = freameData.slice(4, 6).toString('hex').toUpperCase();//设备类型ID
            if ("C001" === deviceTypeId) {//判断是本服务设备类型
                // console.log(`${new Date().toLocaleString()}`, 'frameId:---->>>>' + frameId, deviceTypeId, "C001");
                switch (frameId) {
                    //(E011)设备上报参数入网，替换存储到设备数据库，默认sn:000000000000000000
                    case 'E011':
                        //查询设备信息
                        // Common.QueryDeviceTypeInfo(deviceTypeId, (err, msg) => {
                        //     if (err === null) {
                        //         console.log(`${new Date().toLocaleString()}`, msg)
                        //         let deviceTypeInfo = msg;
                        //         let deviceType = deviceTypeInfo.deviceType;
                        //         if (deviceType == "Actuator") {
                        //             //执行器设备入网
                        //             Common.ActuatorApplyForNetworkAccess(addr, freameData, deviceTypeInfo);
                        //         } else if (deviceType == "Sensor") {
                        //             //传感器设备入网
                        //             Common.SensorApplyForNetworkAccess(addr, freameData, deviceTypeInfo);
                        //         }
                        //     } else {
                        //         console.log(`${new Date().toLocaleString()}`, err, msg)
                        //     }
                        // });
                        break;
                    case 'E012':
                        //执行器请求设置工作组及区域编号
                        // Common.QueryDeviceTypeInfo(deviceTypeId, (err, msg) => {
                        //     if (err === null) {
                        //         console.log(`${new Date().toLocaleString()}`, msg)
                        //         let deviceTypeInfo = msg;
                        //         let deviceType = deviceTypeInfo.deviceType;
                        //         if (deviceType == "Actuator") {
                        //             //执行器设备请求设置工作组及区域编号
                        //             console.log(`${new Date().toLocaleString()}`, `类型${deviceType}（${deviceTypeId}）请求设置工作组`)
                        //             Common.ActuatorApplaySetGroupIdArray(addr, freameData);
                        //         } else {
                        //             console.log(`${new Date().toLocaleString()}`, `类型${deviceType}（${deviceTypeId}）不存在设置设备工作组及区域编号功能`);
                        //         }
                        //     } else {
                        //         console.log(`${new Date().toLocaleString()}`, err, msg)
                        //     }
                        // });
                        break;
                    case 'E013':
                        //设备请求设置Sn
                        // Common.QueryDeviceTypeInfo(deviceTypeId, (err, msg) => {
                        //     if (err === null) {
                        //         console.log(`${new Date().toLocaleString()}`, msg)
                        //         let deviceTypeInfo = msg;
                        //         let deviceType = deviceTypeInfo.deviceType;
                        //         if (deviceType == "Actuator") {
                        //             //执行器设备请求设置Sn
                        //             console.log(`${new Date().toLocaleString()}`, `类型${deviceType}（${deviceTypeId}）请求设置sn`)
                        //             Common.ActuatorApplaySetDeviceSnAndChannel(addr, freameData);
                        //         } else if (deviceType == "Sensor") {
                        //             console.log(`${new Date().toLocaleString()}`, `类型${deviceType}（${deviceTypeId}）请求设置sn`)
                        //             Common.SensorApplaySetDeviceSnAndChannel(addr, freameData);
                        //         } else {
                        //             console.log(`${new Date().toLocaleString()}`, `类型${deviceType}（${deviceTypeId}）不存在设置设备Sn功能`);
                        //         }
                        //     } else {
                        //         console.log(`${new Date().toLocaleString()}`, err, msg)
                        //     }
                        // });
                        break;
                    case 'E014':
                        //设备上报实时工作状态
                        S100.WorkingStatusParser(RedisGateway, addr, glsnr, grssi, freameData);
                        break;
                    case 'E015':
                        //设备通用回执
                        S100.CommonAckStatusParser(RedisGateway, addr, freameData);
                        break;
                    case 'E017'://设定S100总行程时间
                        console.log(freameData)
                        let e17_channel = freameData.slice(7, 8)[0];
                        let e17_totalOpenTime = freameData.readUInt16BE(8);
                        let e17_totalCloseTime = freameData.readUInt16BE(10);
                        S100.SET_S100_SET_TIME_STATUS_OBJ(`S100SetTotalTime:${addr}_${e17_channel.toString().padStart(2, "0")}`, { "value": 1, "channel": e17_channel, "totalOpenTime": e17_totalOpenTime, "totalCloseTime": e17_totalCloseTime });
                        break;
                    case 'E018'://读取S100总行程时间
                        console.log(freameData)
                        let channel = freameData.slice(7, 8)[0];
                        let totalOpenTime = freameData.readUInt16BE(8);
                        let totalCloseTime = freameData.readUInt16BE(10);
                        S100.SET_S100_READ_TIME_STATUS_OBJ(`S100ReadTotalTime:${addr}_${channel.toString().padStart(2, "0")}`, { "value": 1, "channel": channel, "totalOpenTime": totalOpenTime, "totalCloseTime": totalCloseTime });
                        break;
                    default:

                        break;
                }
            } else if ("0001" === deviceTypeId) {//WS100传感器数据
                switch (frameId) {
                    case 'D001':
                        //FE D001 30 0001 0D 711786E2 7063E2 00000016E0 FFFFFFFFE1 0000E0 FFFFE0 FFFFE0 FFFFE2 FFFFE2 FFFFE0 FFFFE0 20190926084505A0
                        //设备通用回执
                        // console.log(freameData.toString('hex').toUpperCase())
                        // WS100.SensorDataParser(RedisGateway, addr, freameData); //LoRa无线十一因素百叶箱A型数据LoRa Wireless 11-Factor Sensor Type A 
                        break;
                    default:

                        break;
                }
            }
        } catch (error) {

        }

    }

}



export = { Parser };
